package com.wealth.lottery.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import com.wealth.lottery.entity.LotteryActivity;
import com.wealth.lottery.entity.LotteryRecord;
import com.wealth.lottery.repository.LotteryRecordDao;
import com.wealth.lottery.service.ILotteryRecordService;
import com.wealth.member.entity.Member;
import com.wealth.order.entity.Order;
import com.wealth.order.vo.OrderVOCenter;
import com.wealth.system.entity.Corporation;

import iaas.enums.YesOrNoEnum;
import iaas.persistence.DynamicSpecifications;
import iaas.persistence.SearchFilter;
import iaas.persistence.SearchFilter.Operator;
import iaas.service.impl.BaseServiceImpl;
import iaas.utils.ResultObject;
/**
 * LotteryRecordServiceImpl
 * @author lihang 2016-06-27
 *
 */
@Component
public class LotteryRecordServiceImpl extends BaseServiceImpl<LotteryRecord> implements ILotteryRecordService{
	@Autowired
	private LotteryRecordDao lotteryRecordDao;
	@SuppressWarnings("finally")
	@Override
	public int getLotteryRecodeNum(Long activityid) throws Exception {
		int num = -1;
		try {
			num=lotteryRecordDao.findByLotteryactivityId(activityid);
		} catch (Exception e) {
			
		}finally {
			return num;
		}
	}

	@Override
	public LotteryRecord addMemberRecode(int status,Long corpid, Long activityid, Long prizeid, Long memberid) throws Exception {
		LotteryRecord record =new LotteryRecord();
		record.setCreateTime(new Date());
		record.setMember(new Member(memberid));
		record.setStatus(status);
		record.setLotteryactivity(new LotteryActivity(activityid));
		record.setCorp(new Corporation(corpid));
		lotteryRecordDao.save(record);
		return record;
	}

	@Override
	public List<LotteryRecord> getLotteryRecordByCorpId(Long corpId) throws Exception {
		return lotteryRecordDao.findByCorpId(corpId);
	}

	/** 查询会员的抽奖记录 */
	@Override
	public List<LotteryRecord> getLotteryRecordByMemberId(Long memberid) throws Exception{
		return lotteryRecordDao.findByMemberId(memberid);
	}

	@Override
	public ResultObject getLotteryRecordByMemberIdPage(Integer pageSize, Integer pageNumber, Long memberId,Long lotteryid)
			throws Exception {
		/** ---- 分页查询 ---*/
		//创建分页请求.
		Sort sort = new Sort(Direction.DESC,"id");
		PageRequest pageRequest = new PageRequest(pageNumber-1, pageSize, sort);
		//创建动态查询条件组合.
		Collection<SearchFilter> filters = new ArrayList<SearchFilter>();
		//过滤:会员
		if(lotteryid>-1){
			filters.add(new SearchFilter("lotteryactivity.id", Operator.EQ, lotteryid));
		}
		filters.add(new SearchFilter("member.id", Operator.EQ, memberId));
		Specification<LotteryRecord> spec = DynamicSpecifications.bySearchFilter(filters, LotteryRecord.class);
		Page<LotteryRecord> page  = lotteryRecordDao.findAll(spec, pageRequest);
		Long count= page.getTotalElements();
		List<LotteryRecord> lotteryRescordList = page.getContent();
		return new ResultObject("000", String.valueOf(count), lotteryRescordList);
	}
}
